// Ionic_SDK
// 
// 保佑没Bug 
//
// Created by AS on 2019/5/21. 
//
// Copyright © 2018 AS. All rights reserved.
// 

#import "CommunityDetailController.h"
#import "UserInfo.h"
#import "ItemTableViewCell.h"
#import "EdgeUILabel.h"
#import "TimeLinePhotoContainerView.h"
#import "ReplyItemCell.h"
#import "ReplyModel.h"
#import "InputView.h"
@interface CommunityDetailController ()<UITableViewDataSource, UITableViewDelegate,InputViewDelegate>
/**  */
@property (nonatomic,strong) NSMutableArray *dataSource;
/**  */
@property (nonatomic,assign) NSInteger page;
/**  */
@property (nonatomic,assign) NSInteger size;
/**  */
@property (nonatomic,strong) UserInfo *user;
/**  */
@property (nonatomic,strong) UITableView *tableView;
/**  */
@property (nonatomic,strong) InputView *inutView;

/**  */
@property (nonatomic,strong) UIActivityIndicatorView *indicatorView;
/**  */
@property (nonatomic,strong) UILabel *noReplyLabel;
/**  */
@property (nonatomic,strong) ReplyModel *replyModel;
@end

@implementation CommunityDetailController

-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(UserInfo *)user{
    if (!_user) {
        _user = [UserInfo getUserInfo];
    }
    return _user;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"交流圈";
    self.view.backgroundColor = HexRGB(0xf3f5f5);
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.width,ScreenHeight - TopHeight - TabBarHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(down_refreshData)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.view addSubview:self.tableView];
    
    self.inutView = [[InputView alloc]initWithFrame:CGRectMake(0, self.tableView.maxY, self.view.width, 49)];
    self.inutView.delegate = self;
    [self.view addSubview:self.inutView];
    
    self.indicatorView = [UIActivityIndicatorView new];
    self.indicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.indicatorView.hidesWhenStopped = YES;
    [self.indicatorView startAnimating];
    [self.tableView addSubview:self.indicatorView];
    
    [self.indicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView).offset(self.modelF.cellHeight + 100);
        make.centerX.equalTo(self.tableView);
        make.width.height.mas_offset(25);
    }];
    [self down_refreshData];
    
    self.noReplyLabel = [UILabel new];
    self.noReplyLabel.font = Font14;
    self.noReplyLabel.textColor = HexRGB(0x888888);
    self.noReplyLabel.text = @"暂无评论";
    self.noReplyLabel.hidden = YES;
    [self.tableView addSubview:self.noReplyLabel];
    
    [self.noReplyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.indicatorView);
    }];
}


-(void)down_refreshData{
    self.page = 1;
    [self getData:YES];
}

-(void)loadMoreData{
    self.page ++;
    [self getData:NO];
}

-(void)getData:(Boolean)isRefresh{

    NSString *url = @"/api/communicationCircleDetail/getCommunicationDetailsByCcId";
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"ccId"] = @(self.modelF.model.Id);
    dict[@"loginToken"] = self.user.loginToken;
    dict[@"page"] = @(self.page);
    dict[@"size"] = @(20);
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        self.indicatorView.hidden = YES;
        if (!error) {
            if (!msg) {
                
                NSLog(@"%@",[result mj_JSONString]);
                NSArray *array = [ReplyModel mj_objectArrayWithKeyValuesArray:result];

                for (int i = 0; i < array.count; i++) {
                    ReplyModel *model = array[i];
                    if (model.detailId) {
                        for (ReplyModel *m in array) {
                            if (model.detailId == m.Id) {
                                model.replyModel = m;
                                break;
                            }
                        }
                    }
                }
                if (array.count < 20) {
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.tableView.mj_footer resetNoMoreData];
                }
                NSMutableArray *mutableArray = [NSMutableArray array];
                for (ReplyModel *m in array) {
                    ReplyModelF *f = [ReplyModelF new];
                    f.model = m;
                    [mutableArray addObject:f];
                }
                if (isRefresh) {
                    [self.dataSource removeAllObjects];
                }
                [self.dataSource addObjectsFromArray:mutableArray];
                if (self.dataSource.count == 0) {
                    self.noReplyLabel.hidden = NO;
                }else{
                    self.noReplyLabel.hidden = YES;
                }
                [self.tableView reloadData];
            }
        }
    }];
}

-(void)inputViewSendText:(NSString *)text{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"content"] = text;
    dict[@"loginToken"] = self.user.loginToken;
    dict[@"replyId"] = @(self.user.uid);
    dict[@"ccId"] = @(self.modelF.model.Id);
    dict[@"detailId"] = self.replyModel ? @(self.replyModel.Id) : @"";
    
    NSString *url = @"/api/communicationCircleDetail/insertCommunicationCircleDetail";
    
    [HTTPTools POST:url parameters:dict callback:^(id result, NSString *msg, NSError *error) {
        
        if (!error) {
            if (!msg) {
                [self getData:YES];
            }
        }
    }];
}

-(void)inputViewDidEndText{
    self.replyModel = nil;
}

#pragma mark - UITableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? 1 : self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        ItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ItemTableViewCell"];
        if (!cell) {
            cell = [[ItemTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ItemTableViewCell"];
        }
        cell.modelF = self.modelF;
        return cell;
    }else{
        ReplyItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReplyItemCell"];
        
        if (!cell) {
            cell = [[ReplyItemCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ReplyItemCell"];
        }
        WeakSelf(weakSelf);
        cell.replyModelBlock = ^(ReplyModel * _Nonnull model) {
            weakSelf.replyModel = model;
            [weakSelf.inutView setPlaceholder:[NSString stringWithFormat:@"回复:%@",model.nickName]];
        };
        
        cell.modelF = self.dataSource[indexPath.row];
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return self.modelF.cellHeight;
    }else{
        
        ReplyModelF *modelF = self.dataSource[indexPath.row];
        
        
        return modelF.cellHeight;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 40.0;
    }else{
        return 0.00001;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        UIView *view = [UIView new];
        view.backgroundColor = HexRGB(0xf3f5f5);
        
        UILabel *label = [UILabel new];
        label.text = @"精彩评论";
        label.font = Font14;
        label.textColor = HexRGB(0x666666);
        [view addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(view).offset(12);
        }];
        
        return view;
    }else{
        return [UIView new];
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
    
}





@end


